package com.eques.view;

import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnSelectTimeTypeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.eques.demo.R;
import com.eques.icvss.utils.ELog;
import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 版权：移康智能科技（上海）股份有限公司版权所有
 *
 * @ProjectName： DingDongProjectSource2019
 * @Package: com.eques.doorbell.ui.activity.voice.util
 * @ClassName: OptPickerView
 * @Description： java类作用描述
 * @Author： guangzhao.huang
 * @CreateDate: 2020/4/10 11:33
 * @Version: 1.0
 */
public class OptPickerView {
    private final String TAG = "OptPickerView";

    private volatile static OptPickerView optPickerView = null;
    private SelectTimeSaveDataImpl selectTimeSaveDataImpl;
    private SelectIntervalTimeImpl selectIntervalTime;
    private OptionsPickerView pvNoLinkOptions;
    private TimePickerView timePickerView;


    //    // 默认开始时间选择类型
//    private boolean startTimeType = true;
    // 初始化标记
    private boolean init = true;
    // 开始时间、结束时间
    private int startOptions1 = 00;
    private int startOptions2 = 00;
    // 默认或者选择保存的时间间隔
    private int intervalTime = 0;

    private int jumpHour = 00;
    private int jumpMinutes = 00;
    // 默认添加的滑动列表数据
    private final List<String> hours = new ArrayList<>();
    private final List<String> minutes = new ArrayList<>();
    // 默认添加的滑动列表数据
    private List<Integer> alarmIntervalTime = new ArrayList<>();
    private final List<String> alarmIntervalTimeStr = new ArrayList<>();
    //默认添加年月日列表
    private final List<String> years = new ArrayList<>();
    private final List<List<String>> months = new ArrayList<>();
    private final List<List<List<String>>> days = new ArrayList<>();

    private WeakReference<Activity> weakReferenceAc = null;

    //当前设置是否是开始时间
    private int type = 0;

    public OptPickerView setActivity(Activity activity) {
        this.weakReferenceAc = new WeakReference<>(activity);
        return optPickerView;
    }

    public static OptPickerView getInstance() {
        if (optPickerView == null) {
            synchronized (OptPickerView.class) {
                if (optPickerView == null) {
                    optPickerView = new OptPickerView();
                }
            }
        }
        return optPickerView;
    }


    public OptPickerView initNotifyTime(SelectTimeSaveDataImpl selectTimeSaveDataImpl) {
        this.selectTimeSaveDataImpl = selectTimeSaveDataImpl;
        initList();
        initNotifyTimePicker();
        return optPickerView;
    }

    public OptPickerView initDateTime(SelectTimeSaveDataImpl selectTimeSaveData) {
        this.selectTimeSaveDataImpl = selectTimeSaveData;
        initDateTimePicker();
        return optPickerView;
    }

    public OptPickerView initAlarmIntervalTime(List<Integer> alarmIntervalTime) {
        this.alarmIntervalTime = alarmIntervalTime;
        initAlarmIntervalListData();
        initAlarmIntervalPicker();
        return optPickerView;
    }

    public OptPickerView saveEventListener(SelectIntervalTimeImpl selectIntervalTime) {
        this.selectIntervalTime = selectIntervalTime;
        return optPickerView;
    }

    /**
     * 添加联动数据
     */
    public void initList() {
        if (hours != null && hours.size() > 0) {
            hours.clear();
        }
        for (int i = 0; i < 24; i++) {
            if (i < 10) {
                hours.add("0" + i);
            } else {
                hours.add(String.valueOf(i));
            }
        }
        if (minutes != null && minutes.size() > 0) {
            minutes.clear();
        }
        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                minutes.add("0" + i);
            } else {
                minutes.add(String.valueOf(i));
            }
        }
    }

    /**
     * 获取报警间隔数据
     */
    public void initAlarmIntervalListData() {
        if (alarmIntervalTimeStr != null) {
            alarmIntervalTimeStr.clear();
        }

        for (int i = 0; i < alarmIntervalTime.size(); i++) {
            alarmIntervalTimeStr.add(alarmIntervalTime.get(i) + weakReferenceAc.get().getResources().getString(R.string.voice_tel_care_alarm_time));
        }

    }

    public void setInit(boolean init) {
        this.init = init;
    }

    /**
     * 初始化已有的开始和结束时间
     *
     * @param startHour
     * @param startMinutes

     */
    public OptPickerView initJumpTime(int startHour, int startMinutes) {
        // 默认开始时间
        this.startOptions1 = startHour;
        this.startOptions2 = startMinutes;
//        String startTimeStr = startHour + "时" + startMinutes + "分";
//        pvNoLinkOptions.setStartEndTime(1, startTimeStr);
//        pvNoLinkOptions.setStartEndTime(2, endTimeStr);
        // 默认开始时间跳转
        autoJumpTime(startOptions1, startOptions2);
        return optPickerView;
    }


    public OptPickerView setTimeType(int type) {
        this.type = type;
        return optPickerView;
    }

    /**
     * 根据选取的开始或结束选项，获得现有的时间点
     *
     * @param jumpHour
     * @param jumpMinutes
     */
    public void autoJumpTime(int jumpHour, int jumpMinutes) {
        ELog.i(TAG, " autoJumpTime() jumpHour: ", jumpHour, " jumpMinutes: ", jumpMinutes);
        this.jumpHour = jumpHour;
        this.jumpMinutes = jumpMinutes;
        autoSelect(jumpHour, jumpMinutes);
    }

    /**
     * 转换和补全单位数时间
     */
    private String completionStarthourStr;
    private String completionStartMinutesStr;
//    private String completionEndhourStr;
//    private String completionEndMinutesStr;


    private void initDateTimePicker() {
        Calendar calendar = Calendar.getInstance();
        Calendar calendar_end = Calendar.getInstance();
        calendar_end.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 20);
        timePickerView = new TimePickerBuilder(weakReferenceAc.get(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                selectTimeSaveDataImpl.saveTimeStr( getYMD(date));
            }
        })
                .setDate(calendar)
                .setContentTextSize(20)//设置滚轮文字大小
                .setLineSpacingMultiplier(2.0f)// 行间距
                .setDividerColor(weakReferenceAc.get().getResources().getColor(R.color.protocol_line_bg))//设置分割线的颜色
                .setTitleBgColor(Color.WHITE)
                .setSubCalSize(16)
                .setRangDate(calendar, calendar_end)
                .setCancelColor(weakReferenceAc.get().getResources().getColor(R.color.service_cloud_plan_three))
                .setSubmitColor(weakReferenceAc.get().getResources().getColor(R.color.service_open_details_color))
                .setTextColorCenter(weakReferenceAc.get().getResources().getColor(R.color.service_cloud_plan_one))
                .setItemVisibleCount(8)
                .setOutSideCancelable(false)
                .build();
        timePickerView.show();
    }

    /**
     * 获取年月日
     *
     * @return
     */
    public String getYMD(Date d) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        return sdf.format(d);
    }


    /**
     * 通知时间
     */
    private void initNotifyTimePicker() {
        pvNoLinkOptions = new OptionsPickerBuilder(weakReferenceAc.get(), new OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                // 确认保存后取消联动组件
                pvNoLinkOptions.dismiss();

                if (startOptions1 < 10) {
                    completionStarthourStr = "0" + startOptions1;
                } else {
                    completionStarthourStr = String.valueOf(startOptions1);
                }
                if (startOptions2 < 10) {
                    completionStartMinutesStr = "0" + startOptions2;
                } else {
                    completionStartMinutesStr = String.valueOf(startOptions2);
                }

                selectTimeSaveDataImpl.saveTimeStr(completionStarthourStr + completionStartMinutesStr);


            }

            @Override
            public void onOptionsSelect(int options1, View v) {
            }
        }, new OnSelectTimeTypeListener() {
            @Override
            public void selectTime(int options1, int options2, View v) {
                // 选择修改开始时间，修改头部样式，开始默认时间跳转
                pvNoLinkOptions.setStartStyly(type);
                autoJumpTime(startOptions1, startOptions2);
            }
        })
                .setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
                    @Override
                    public void onOptionsSelectChanged(int options1, int options2, int options3) {
                        ELog.i(TAG, " 滑动调节 onOptionsSelectChanged() options1: ", options1, " options2: ", options2);
                        String str = hours.get(options1) + "时" + minutes.get(options2) + "分";
//                            pvNoLinkOptions.setStartEndTime(1, str);
                        // 调节开始时间选取赋值
                        startOptions1 = options1;
                        startOptions2 = options2;
                    }

                    @Override
                    public void onOptionsSelectChanged(int options1, int options2) {
                    }

                    @Override
                    public void onOptionsSelectChanged(int options1) {
                    }
                })
                .setContentTextSize(20)//设置滚轮文字大小
                .setLineSpacingMultiplier(2.0f)// 行间距
                .setDividerColor(weakReferenceAc.get().getResources().getColor(R.color.protocol_line_bg))//设置分割线的颜色
                .setTitleBgColor(Color.WHITE)
                .setSubCalSize(16)
                .setCancelColor(weakReferenceAc.get().getResources().getColor(R.color.service_cloud_plan_three))
                .setSubmitColor(weakReferenceAc.get().getResources().getColor(R.color.service_open_details_color))
                .setTextColorCenter(weakReferenceAc.get().getResources().getColor(R.color.service_cloud_plan_one))
                .setItemVisibleCount(8)
                .setOutSideCancelable(false)
                .build();
        pvNoLinkOptions.setNPicker(hours, minutes);
    }

    /**
     * 报警间隔
     */
    public void initAlarmIntervalPicker() {
        pvNoLinkOptions = new OptionsPickerBuilder(weakReferenceAc.get(), new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                // 返回的分别是三个级别的选中位置，只需要取第一个值下标
                ELog.i(TAG, " 保存 onOptionsSelect() options1: ", options1);
                selectIntervalTime.saveIntervalTime(intervalTime);
                pvNoLinkOptions.dismiss();
            }

            @Override
            public void onOptionsSelect(int options1, View v) {
            }
        }, null).setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
            @Override
            public void onOptionsSelectChanged(int options1, int options2, int options3) {
                ELog.i(TAG, " 滑动调节 onOptionsSelectChanged() options1: ", options1);
                Integer integer = alarmIntervalTime.get(options1);
                if (integer > 0) {
                    // 小时转换为分钟
                    intervalTime = integer * 60;
                }
            }

            @Override
            public void onOptionsSelectChanged(int options1, int options2) {
            }

            @Override
            public void onOptionsSelectChanged(int options1) {
            }
        })
                .setTitleText("设置报警间隔")
                .setContentTextSize(20)//设置滚轮文字大小
                .setTitleSize(18)
                .setSubCalSize(14)
                .setLineSpacingMultiplier(2.0f)// 行间距
                .setDividerColor(weakReferenceAc.get().getResources().getColor(R.color.protocol_line_bg))//设置分割线的颜色
                .setSelectOptions(0)//默认选中项
                .setBgColor(Color.WHITE)
                .setTitleBgColor(Color.WHITE)
                .setTitleColor(weakReferenceAc.get().getResources().getColor(R.color.settings_left_tv_color))
                .setCancelColor(weakReferenceAc.get().getResources().getColor(R.color.service_cloud_plan_three))
                .setSubmitColor(weakReferenceAc.get().getResources().getColor(R.color.settings_restart_btn_bg))
                .setTextColorCenter(weakReferenceAc.get().getResources().getColor(R.color.settings_left_tv_color))
                .isRestoreItem(true)//切换时是否还原，设置默认选中第一项。
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .build();
        pvNoLinkOptions.setPicker(alarmIntervalTimeStr);//一级选择器
        pvNoLinkOptions.show();
    }


    /**
     * 展示联动组件
     */
    public OptPickerView show() {
        pvNoLinkOptions.setStartStyly(type);
        pvNoLinkOptions.showTimeParentLayout(true);
        pvNoLinkOptions.show();
        return optPickerView;
    }

    /**
     * 根据之前所选取时间数据，自动给联动器赋值并跳到对应时间点
     */
    public void autoSelect(int jumpHour, int jumpMinutes) {
        pvNoLinkOptions.setSelectOptions(jumpHour, jumpMinutes);
    }


    public OptPickerView autoSelect(int index) {
        pvNoLinkOptions.setSelectOptions(index);
        return optPickerView;
    }

    /**
     * 获取这个月有多少天
     *
     * @param year
     * @param month
     * @return
     */
    private int getMonthLastDay(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.DATE, -1);
        return calendar.get(Calendar.DATE);
    }

}
